<?php

namespace frontend\models;

use Yii;
use yii\helpers\VarDumper;

/**
 * This is the model class for table "goods_category".
 *
 * @property string $id
 * @property integer $tree
 * @property string $lft
 * @property string $rgt
 * @property string $depth
 * @property string $name
 * @property string $parent_id
 * @property string $intro
 */
class GoodsCategory extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'goods_category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['tree', 'lft', 'rgt', 'depth', 'parent_id'], 'integer'],
            [['parent_id'], 'required'],
            [['intro'], 'string'],
            [['name'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'tree' => '树的编号',
            'lft' => '左边界',
            'rgt' => '右边界',
            'depth' => '级别',
            'name' => '名称',
            'parent_id' => '上级分类',
            'intro' => '简介',
        ];
    }


    public function getChildren(){
        return $this->hasMany(self::className(),['id'=>'parent_id']);
    }



    public static function getGoodsCategorys($expand=''){
        //获取所有数据的数组
        $gcates = GoodsCategory::find()->asArray()->all();
echo <<<NAVBAR
    <div class="nav w1210 bc mt10">
        <!--  商品分类部分 start-->
      <div class="category fl"> <!-- 非首页，需要添加cat1类 -->
            <div class="cat_hd {$expand}">  <!-- 注意，首页在此div上只需要添加cat_hd类，非首页，默认收缩分类时添加上off类，鼠标滑过时展开菜单则将off类换成on类 -->
                <h2>全部商品分类</h2>
                <em></em>
            </div>

            <div class="cat_bd">  

NAVBAR;

        $i =0;
        foreach($gcates as $navbar){
            //循环一级导航条
        if($navbar['depth']==0){
                echo $i===0?'<div class="cat items">':'<div class="cat">';
                $i+=1;
                echo '<h3><a href="">'.$navbar['name'].'</a><b></b></h3><div class="cat_detail none">';
                //循环二级导航条
                foreach($gcates as $navbar2){
                    if($navbar2['parent_id']==$navbar['id']){
                        echo '<dl class="dl_1st"><dt><a href="">'.$navbar2["name"].'</a></dt><dd>';
                        //循环三级导航条
                   foreach ($gcates as $navbar3){
                       if($navbar3['parent_id']==$navbar2['id']){
                           echo '<a href="">'.$navbar3['name'].'</a>';
                       }
                   }
                echo '</dd></dl>';
                    }
                }
        echo '</div></div>';
        }
        }

echo <<<NAVBAR

            </div>
        </div>


NAVBAR;


        //返回数据
        //return $gcates;


    }
}
